home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / c / winet.com / PROFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-04  |  6.4 KB  |  236 lines

  1. #include <stdio.h>
  2. #include <process.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys\types.h>
  6. #include <sys\stat.h>
  7. #include <dgb.h>
  8. #include <exc.h>
  9. #include "types.h"
  10. #include "profile.h"
  11. #include "segments.h"
  12. #include "symfile.h"
  13. #include "segbug.h"
  14.  
  15. PSTR OutputFileName = NULL;
  16. PSTR RunFile = "WIN2";
  17. PSTR TimerPeriodString = NULL;
  18. WORD TimerPeriod = 100;
  19.  
  20. char Usage[] = "Usage: %s [-?] [-t] [-o] [-x] [Symbol files ...] [WIN2.COM argument]\n"
  21.            "-t Time in milliseconds (default 100ms)\n"
  22.            "-o Output file (defaults to stdout)\n"
  23.            "-x Program to run (default WIN2.COM)\n"
  24.            "Symbol files must end in '.SYM'\n"
  25.            "This program requires PC-DOS v3.x/IBM PC-AT or compatible\n\n"
  26.            "Windows execution profiler V1.03   DGB\n\n";
  27.  
  28. char Copyright[] =
  29. "Execution Timer for Microsoft Windows v1.0\n\
  30. (c) Duncan G. Booth RCP Ltd. 1987\n\n\
  31. \tRCP Ltd.\n\
  32. \tBrookside\n\
  33. \tWestbrook Street\n\
  34. \tBlewbury\n\
  35. \tDidcot\n\
  36. \tOxon OX11 9QA\n\
  37. \tUnited Kingdom\n\n\
  38. Permission is granted to copy this program for non-commercial use only.\n\
  39. For commercial distribution, update information or program source please\n\
  40. contact D.Booth at the above address or 'jrichards' on BIX.\n";
  41.  
  42. exception BAD_ARGS = "Bad arguments";
  43. exception BAD_SYMBOL = "Cannot find symbol!";
  44. exception FINDFILE_FAILED = "Cannot find file";
  45.  
  46. void *emalloc(WORD);
  47. PSTR safestrdup(PSTR);
  48.  
  49. void *emalloc(w)
  50. WORD w;
  51. {
  52.    char *p = malloc(w);
  53.    if (p == NULL) exc_raise(OUT_OF_MEMORY);
  54.    return p;
  55. }
  56. char *safestrdup(s)
  57. register char *s;
  58. {
  59.    s = strdup(s);
  60.    if (s == NULL) exc_raise (OUT_OF_MEMORY);
  61.    return s;
  62. }
  63.  
  64. #define FNAMESIZE 128
  65. char *pathseps = "\\:/";    /* Valid pathname separators */
  66. char *pathdelim = "; ";        /* Environment path delimiters */
  67. /* This procedure will find a file matching the given name.
  68.    a) The name is extended with 'ext' unless it already has an extension or
  69.       force is specified. (ext should include the '.')
  70.    b) The current directory is checked for the file
  71.    c) Unless the file has a path associated, the directories in paths are
  72.       searched for the file. paths is a semicolon separated list.
  73.    Flags: force - strip existing extension and add new.
  74.       output - file is for output. Just do extension processing.
  75.  */
  76.  
  77. char *findfile (paths, name, ext, force, output)
  78. char *paths, *name, *ext;
  79. int force, output;
  80. {
  81.    register char *p, *q;
  82.    int namelen;
  83.    int haspath = FALSE;
  84.    char filename[FNAMESIZE]; /* Filename without path. */
  85.    char longname[FNAMESIZE]; /* Filename with path. */
  86.    struct stat dummy;
  87.    strcpy(filename, name);
  88.    for (p = q = filename; (q = strpbrk(p, pathseps)) != NULL; p = q+1)
  89.      haspath = TRUE;
  90.  
  91.    /* p points to start of name part of filename */
  92.    if ((q = strchr(p, '.')) == NULL || force)
  93.    {
  94.       if (q != NULL) *q = '\0'; /* Remove old extension */
  95.       strcat(p, ext);
  96.    }
  97.    if (output || stat(filename, &dummy) == 0) return strlwr(safestrdup (filename));
  98.  
  99.    if (haspath || paths == NULL)
  100.    {
  101.       BadFile = name;
  102.       exc_raise (FINDFILE_FAILED);
  103.    }
  104.  
  105.    paths = safestrdup(paths); /* Because strtok splats this string. */
  106.  
  107.    /* Now search path string: */
  108.    p = strtok(paths, pathdelim);
  109.    do
  110.    {
  111.       strcpy(longname, p);
  112.       strcat(longname, "\\");
  113.       strcat(longname, filename);
  114.       if (stat(longname, &dummy) == 0)
  115.       {
  116.      free(paths);
  117.      return strlwr(safestrdup (longname));
  118.       }
  119.    } while ((p = strtok(NULL, pathdelim)) != NULL);
  120.    free(paths);
  121.    BadFile = name;
  122.    exc_raise(FINDFILE_FAILED);
  123. }
  124.  
  125. /*
  126.  * Although this program is compiled SS != DS, functions outside the
  127.  * debug internals do actually have SS==DS. In particular this means
  128.  * I can call system from here.
  129.  */
  130. void main (argc, argv)
  131. int argc;
  132. char **argv;
  133. {
  134.    char *path = getenv ("PATH");
  135.    char *progname = argv[0];
  136.    char cmdline[128];
  137.    BEGIN
  138.       {
  139.      register char *s;
  140.      register char *q;
  141.      for (s = argv[0]; (q = strpbrk(s, ":/\\")) != NULL;) s = q+1;
  142.  
  143.      if (*s != 0)
  144.      {
  145.         progname = s = strlwr(safestrdup(s));
  146.         if ((q = strchr(s, '.')) != NULL) *q = 0;
  147.      }
  148.       }
  149.  
  150.       if (_osmajor < 3) exc_raise(BAD_ARGS);
  151.       while (argc > 1 && argv[1][0] == '-')
  152.       {
  153.      char *p, **pp;
  154.      switch (argv[1][1])
  155.      {
  156.         case 0: exc_raise(BAD_ARGS);
  157.         case 'x': case 'X':
  158.            pp = &RunFile;
  159.            break;
  160.         case 'o': case 'O':
  161.            pp = &OutputFileName;
  162.            break;
  163.         case 't': case 'T':
  164.            pp = &TimerPeriodString;
  165.            break;
  166.         case '?':
  167.            fprintf(stderr, Usage, progname);
  168.            fprintf(stderr, Copyright);
  169.            exit(0);
  170.      }
  171.      *pp = &argv[1][2];
  172.      argv++; argc--;
  173.      if (**pp == 0)
  174.      {
  175.         unless (argc > 1) exc_raise(BAD_ARGS);
  176.         *pp = &argv[1][0];
  177.         argv++; argc--;
  178.      }
  179.       }
  180.       
  181.       if (TimerPeriodString != NULL)
  182.      TimerPeriod = atoi(TimerPeriodString);
  183.       if (TimerPeriod == 0) exc_raise(BAD_ARGS);
  184.  
  185.       while (argc > 1 && IsSymFileName (argv[1]))
  186.       {
  187.      char *fname = findfile(path, argv[1], ".sym", FALSE, FALSE);
  188.      LoadSymFile(fname);
  189.      argv++; argc--;
  190.       }
  191.       DefineSegment ((LPSTR) "*BIOS*", 0, 0xf000, 0xffff, 0xf000);
  192.       DefineSegment ((LPSTR) "*DOS*", 0, 0x70, 0xffff, 0x70);
  193.       DefineSegment ((LPSTR) "*EGA*", 0, 0xc000, 0xffff, 0xc000);
  194.  
  195.       strcpy (cmdline, RunFile);
  196.       while (argc > 1)
  197.       {
  198.      strcat(cmdline, " ");
  199.      strcat(cmdline, argv[1]);
  200.      argv++; argc--;
  201.       }
  202.  
  203.       LockTimer(TRUE);
  204.       InitDebugSeg (TimerPeriod);
  205.       fprintf(stderr, "Running: %s\n", cmdline);
  206.       system(cmdline);
  207.       LockTimer(TRUE);
  208.  
  209.       ClearDebugSeg();
  210.       fprintf(stderr, "Statistics...\n\n");
  211.       PrintSegments();
  212.       fprintf(stderr, "Finished\n");
  213.    EXCEPTION
  214.       WHEN(OUT_OF_MEMORY OR_WHEN CONTROL_C OR_WHEN BAD_SYMBOL)
  215.      fprintf(stderr, "%s\n", (char *) exc_code);
  216.          exit(1);
  217.       WHEN(BAD_ARGS)
  218.      fprintf(stderr, "%s\n", (char *) exc_code);
  219.      fprintf(stderr, Usage, progname);
  220.          fprintf(stderr, Copyright);
  221.      exit(1);
  222.       WHEN(FINDFILE_FAILED)
  223.      fprintf(stderr, "%s: %s\n", (char *) exc_code, BadFile);
  224.          exit(1);
  225.       WHEN(OP_FAILED)
  226.      fprintf(stderr, "%s ", progname);
  227.          if (BadFile == NULL) BadFile = "";
  228.      perror(BadFile);
  229.          exit(1);
  230.       WHEN (OTHERS)
  231.      fprintf(stderr, "Unexpected exception: %s\n", (char *) exc_code);
  232.          exit(1);
  233.    END
  234.    exit(0);
  235. }
  236.